package org.tema.web;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

import org.tema.ejb.ProductBeanLocal;
import org.tema.entities.Product;

public class ProductConverter implements Converter {
	
	private ProductBeanLocal productBean;

	public ProductConverter(ProductBeanLocal productBean) {
		this.productBean = productBean;
	}

	public Object getAsObject(FacesContext context, UIComponent component, String newValue) {
		return productBean.findProductByName(newValue);
	}

	public String getAsString(FacesContext context, UIComponent component, Object value) {
		return (value == null) ? null : ((Product) value).getName();
	}

}
